package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.domain.UserPo;
import com.tan.steamship.dto.UserDto;
import com.tan.steamship.exception.ExceptionEnum;
import com.tan.steamship.exception.XmException;
import com.tan.steamship.mapper.UserMapper;
import com.tan.steamship.service.UserService;
import com.tan.steamship.util.MD5Util;
import com.tan.steamship.vo.UserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 85726
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-12-10 20:30:35
*/
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo>
    implements UserService{
    private final UserMapper userMapper;

    @Override
    public UserPo login(UserPo user) {
        user.setPassword(MD5Util.MD5Encode(user.getPassword() + "", "UTF-8"));
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getPassword, user.getPassword());
        queryWrapper.eq(UserPo::getName, user.getName()).last("limit 1");
        UserPo userPo = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(userPo)) {
            throw new XmException(ExceptionEnum.GET_USER_NOT_FOUND);
        }
        return userPo;
    }

    @Override
    public void register(UserDto userDto) {
        UserPo user = BeanUtil.copyProperties(userDto, UserPo.class);
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getName, user.getName());
        List<UserPo> userPos = this.list(queryWrapper);
        // 先去看看用户名是否重复
        if (CollUtil.isNotEmpty(userPos)) {
            // 用户名已存在
            throw new XmException(ExceptionEnum.SAVE_USER_REUSE);
        }
        // 使用md5对密码进行加密
        user.setPassword(MD5Util.MD5Encode(user.getPassword() + "", "UTF-8"));
        // 存入数据库
        try {
            if ("admin".equals(user.getName())) {
                user.setType(1);
            }else {
                user.setType(0);
            }
            this.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new XmException(ExceptionEnum.SAVE_USER_ERROR);
        }
    }

    @Override
    public void saveOrUpdate(UserDto userDto) {
        UserPo user = BeanUtil.copyProperties(userDto, UserPo.class);
        if (ObjectUtil.isNotEmpty(user.getId())) {
            // 更新
            user.setUpdatedTime(new Date());
        }
        this.saveOrUpdate(user);
    }

    @Override
    public Page<UserPo> listUser(String userName, int page, int size) {
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(userName)) {
            queryWrapper.eq(UserPo::getName, userName);
        }
        queryWrapper.orderByDesc(UserPo::getCreatedTime);
        return this.page(new Page<>(page, size), queryWrapper);
    }

    @Override
    public UserVo detail(String id) {
        UserPo userPo = this.getById(id);
        if (ObjectUtil.isEmpty(userPo)) {
            return null;
        }
        UserVo userVo = BeanUtil.copyProperties(userPo, UserVo.class);
        return userVo;
    }

    @Override
    public void isExist(String username) {
        LambdaQueryWrapper<UserPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPo::getName, username);
        if (this.count(queryWrapper) > 0) {
            throw new XmException(ExceptionEnum.SAVE_USER_REUSE);
        }
    }

}




